package com.demo.redis.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * @Configuration 先关闭，不然会影响到老的连接
 * 多个连接实例
 * @author peter
 * @date 2022/2/15 11:09
 */
@Configuration
@SuppressWarnings({"unchecked", "rawtypes"})
public class MutiConnRedisConfiguration {

    /**
     * 第一步：配置连接池
     * @return GenericObjectPoolConfig
     */
    @Bean
    @ConfigurationProperties(prefix = "redis.pool")
    @Scope(value = "prototype")
    public GenericObjectPoolConfig redisPool() {
        return new GenericObjectPoolConfig();
    }

    /**
     * 第二步：读取yaml配置redis连接信息, 第一个数据源
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public RedisStandaloneConfiguration redisConfigDefault(){
        return new RedisStandaloneConfiguration();
    }

    /**
     * 第二步：读取yaml配置redis连接信息， 第二个数据源
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "redis.redis-one")
    public RedisStandaloneConfiguration redisConfigOne(){
        return new RedisStandaloneConfiguration();
    }

    /**
     * 第三步：读取自定义连接信息配置，连接池配置，生成LettuceConnectionFactory 工厂
     * @param poolConfig 连接池配置信息
     * @param redisConfigOne redis连接信息
     * @return LettuceConnectionFactory 工厂实例
     */
    @Bean(name = "redisFactoryDefault")
    @Primary
    public LettuceConnectionFactory redisFactoryDefault(GenericObjectPoolConfig poolConfig,
                                               @Qualifier("redisConfigDefault") RedisStandaloneConfiguration redisConfigOne){

        LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration
                .builder()
                .poolConfig(poolConfig)
                .commandTimeout(Duration.ofMillis(poolConfig.getMaxWaitMillis()))
                .build();

        return new LettuceConnectionFactory(redisConfigOne, clientConfiguration);
    }

    /**
     * 第三步：读取自定义连接信息配置，连接池配置，生成LettuceConnectionFactory 工厂
     * @param poolConfig 连接池配置信息
     * @param redisConfigOne redis连接信息
     * @return LettuceConnectionFactory 工厂实例
     */
    @Bean(name = "redisFactoryOne")
    public LettuceConnectionFactory redisFactoryOne(GenericObjectPoolConfig poolConfig,
                                               @Qualifier("redisConfigOne") RedisStandaloneConfiguration redisConfigOne){

        LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration
                .builder()
                .poolConfig(poolConfig)
                .commandTimeout(Duration.ofMillis(poolConfig.getMaxWaitMillis()))
                .build();

        return new LettuceConnectionFactory(redisConfigOne, clientConfiguration);
    }

    /**
     * 第四步：LettuceConnectionFactory工厂自动装配redisTemplate
     *
     * 注：@Qualifier 手动注入 redisFactoryOne
     * @return RedisTemplate
     */
    @Bean(name = "redisTemplateOne")
    @ConditionalOnMissingBean(name = "redisTemplateOne")
    public RedisTemplate<Object, Object> redisTemplate(@Qualifier("redisFactoryOne") LettuceConnectionFactory redisConnectionFactory) {

        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(redisConnectionFactory);

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redisTemplate;
    }
}
